package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.constant.HttpResultEnum;
import com.dongdongshop.constant.RedisAdvertConst;
import com.dongdongshop.data.DataResult;
import com.dongdongshop.pojo.content.TbContent;
import com.dongdongshop.pojo.content.TbContentCategory;
import com.dongdongshop.service.content.ContentCategoryService;
import com.dongdongshop.service.content.ContentService;
import com.dongdongshop.util.FileUpload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
* 广告
* */
@Controller
@RequestMapping("content")
public class ContentController {

    @Reference
    private ContentService contentService;

    @Reference
    private ContentCategoryService contentCategoryService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 注入oss工具类
     */
    @Autowired
    private FileUpload fileUpload;

    private static final Logger log = LoggerFactory.getLogger(ContentController.class);

    /*
    * 跳转分类管理页
    * */
    @RequestMapping("toContentCategory")
    public String toContentCategory(Model model){
        //查询所有广告分类信息
        List<TbContentCategory> ccs = contentCategoryService.findContentCategoryAll();
        model.addAttribute("ccs",ccs);
        return "content/contentCategory";
    }

    /*
    * 添加广告分类
    * */
    @RequestMapping(value = "addContentCategory",method = RequestMethod.POST)
    @ResponseBody
    public DataResult addContentCategory(TbContentCategory cc){
        try {
            contentCategoryService.addContentCategory(cc);
        }catch (Exception e){
            log.error("添加广告分类异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
    }
    /*
    * 修改广告分类
    * */
    @RequestMapping(value = "updateContentCategoryById",method = RequestMethod.POST)
    @ResponseBody
    public DataResult updateContentCategoryById(TbContentCategory cc){
        try {
            contentCategoryService.updateContentCategoryById(cc);
        }catch (Exception e){
            log.error("广告分类修改异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
    }

    /*
    * 跳转广告管理页
    * */
    @RequestMapping("toContent")
    public String toContent(Model model){
        //查询所有的广告信息
        List<TbContent> cs = contentService.findContentAll();
        model.addAttribute("cs",cs);
        //查询全部广告分类
        List<TbContentCategory> ccs = contentCategoryService.findContentCategoryAll();
        model.addAttribute("ccs",ccs);
        return "content/content";
    }

    /*
    * 上传图片
    * */
    @RequestMapping(value = "upLoadFile",method = RequestMethod.POST)
    @ResponseBody
    public DataResult upLoadFile(@RequestParam(value = "myFile") MultipartFile myFile){
        String url = null;
        try {
            url = fileUpload.upload(myFile);
        }catch (Exception e){
            log.error("上传图片失败",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage(),url);
    }
    /*
    * 增加广告
    * */
    @RequestMapping(value = "addContent",method = RequestMethod.POST)
    @ResponseBody
    public DataResult addContent(TbContent c){
        try {
            contentService.addContent(c);
            //数据库同步缓存
            redisSync(c.getCategoryId());
        }catch (Exception e){
            log.error("增加广告异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
    }

    /*
    * 根据主键id查询广告信息
    * */
    @RequestMapping(value = "findContentById",method = RequestMethod.POST)
    @ResponseBody
    public DataResult findContentById(Long id,Model model){
        Map<String,Object> map = new HashMap<>();
        try {
            //根据主键id查询广告信息
            TbContent content = contentService.findContentById(id);
            //查询所有广告分类
            List<TbContentCategory> ccs = contentCategoryService.findContentCategoryAll();
            map.put("content",content);
            map.put("ccs",ccs);
        }catch (Exception e){
            log.error("根据id查询广告信息异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage(),map);
    }

    /*
    * 修改广告信息
    * */
    @RequestMapping(value = "updateContentById",method = RequestMethod.POST)
    @ResponseBody
    public DataResult updateContentById(TbContent c){
        try {
            contentService.updateContentById(c);
            //数据库与缓存同步
            redisSync(c.getCategoryId());
        }catch (Exception e){
            log.error("修改广告信息异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
    }

    /*
    * 数据库与缓存同步广告信息
    * */
    private void redisSync(Long categoryId) {
        try {
            //先删除该类型广告缓存
            redisTemplate.boundHashOps(RedisAdvertConst.INDEXADVER).delete(RedisAdvertConst.CATEGORY+categoryId);
            //去数据库查询该类型广告
            List<TbContent> contents = contentService.findContentByCategoryId(categoryId);
            //防止没有删掉缓存，再次删除缓存
            Thread.sleep(200);
            redisTemplate.boundHashOps(RedisAdvertConst.INDEXADVER).delete(RedisAdvertConst.CATEGORY+categoryId);
            //将新的数据添加到缓存中
            redisTemplate.boundHashOps(RedisAdvertConst.INDEXADVER).put(RedisAdvertConst.CATEGORY+categoryId,contents);
        }catch (Exception e){
            log.error("数据库与缓存同步广告信息异常",e);
        }
    }
}
